<html>
<head>
<LINK REL="SHORTCUT ICON" 
HREF="favicon.ico">
<title>List</title>
</head>
<body>
	<?php
		require 'connect.php';

		function getCommand($field, $value){
			if($field == "name"){
				return ("SELECT * FROM People WHERE FirstName LIKE '%".$value."%' OR SecondName LIKE '%".$value."%'; ");
			}
			else if($field == "address"){
				return("SELECT * FROM People WHERE Address LIKE '%".$value."%';");
			}
			else if($field == "number"){
				return ("Select * FROM People WHERE Number LIKE '%".$value."%';");
			}
			else if($field == "email"){
				return ("SELECT * FROM People WHERE Email LIKE '%".$value."%';");
			}
			else{
				return "uhoh";
			}
		}
		if($_POST == null || ($_POST != null && (!isset($_POST['opt']) || $_POST['val'] == ""))){
			if($_POST != null){
				echo "<font color=\"red\"><b>Search entry incomplete, please fill select search field and enter value</b></font><br />";
			}

			echo <<< END
			<b>Search by:</b>
        		<form method="POST" action="list.php">
                	Name <input type="radio" name="opt" value="name" />
                	Address <input type="radio" name="opt" value="address" />
                	Phone Number <input type="radio" name="opt" value="number" />   
                	E-Mail <input type="radio" name="opt" value="email" />
                	Branch <input type="radio" name="opt" value="branch" /><br /><br /><br />
                	<b>Search value: </b><input type = "Text" NAME="val" /><br />
               		 <input type="submit" value="Submit" />
		        </form>
END;
		}
				

		else{
			$query = getCommand($_POST['opt'],$_POST['val']);
			$result = mysql_query($query);
			echo $query;
			//echo mysql_result($result,3);
			if(!$result){
				echo "<br /><br />False<br />";
			}
			else{
				for($i = 0; mysql_result($result,$i) != null; $i = $i + 1){
					$row = mysql_result($result,$i);
					for($j = 0; /*$row[$j] != null*/ $j < 4; $j = $j + 1){
						echo "<br />". $row[$j]."<br />";
					}
				}
			}
			echo "<br /><br />";
			echo "<br /><br />Results for <b>".$_POST['opt']."</b> with value similar to: <b>".$_POST['val']."</b>";
			echo "<br /><br />These results will be clickable - eg click to search for events etc linked to these people";
		}
		echo "<br /><a href=\"index.php\">Home</a>";
		
	?>
</body>
</html>
